package com.yznote.cloudesk.user.server.impl;

import com.yznote.cloudesk.common.constants.YConstant;
import com.yznote.cloudesk.common.oss.OssUtils;
import com.yznote.cloudesk.common.pojo.Personality;
import com.yznote.cloudesk.common.tokenShare.YTokenShare;
import com.yznote.cloudesk.common.utils.IdUtils;
import com.yznote.cloudesk.user.mapper.PersonalityMapper;
import com.yznote.cloudesk.user.server.PersonalityServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * @author : xiaozhoutongxue
 * @date : 2022/11/14 21:17
 */

@Service
public class PersonalityServerImpl implements PersonalityServer {

    @Autowired
    PersonalityMapper personalityMapper;
    @Autowired
    YTokenShare yTokenShare;
    @Autowired
    OssUtils ossUtils;


    @Override
    public Personality getPersonality() {
        Personality personality = personalityMapper.getPersonality(yTokenShare.get().getUserId());
        return personality;
    }

    @Override
    public String insertPersonality(Personality personality) {
        Long userId = yTokenShare.get().getUserId();
        personality.setUserId(userId);
        personality.setCreateBy(userId);
        personality.setCreateTime(LocalDateTime.now());
        personalityMapper.insertPersonality(personality);
        return YConstant.SUCCESS;
    }

    @Override
    public String updatePersonality(Personality personality) {
        Long userId = yTokenShare.get().getUserId();
        personality.setUserId(userId);
        personality.setUpdateBy(userId);
        personality.setUpdateTime(LocalDateTime.now());
        personality.setUserId(userId);
        personalityMapper.updatePersonality(personality);
        return YConstant.SUCCESS;
    }

    @Override
    @Transactional
    public void pushUserHeaders(MultipartFile mtf) throws Exception {
        String name =mtf.getOriginalFilename();
        byte[] bytes = mtf.getBytes();
        Long userId = yTokenShare.get().getUserId();
        String path ="filesystem/"+userId+"/headrs/"+ IdUtils.nextId()+name.substring(name.lastIndexOf("."));
        Personality personality =new Personality();
        personality.setUserHeadsPath(path);
        personality.setUserId(userId);
        personality.setUpdateBy(userId);
        personality.setUpdateTime(LocalDateTime.now());
        personalityMapper.updateheadrs(personality);
        ossUtils.pushFileSource(bytes,path);
    }

    @Override
    @Transactional
    public void pushWallpaperPath(MultipartFile mtf) throws Exception{
        String name =mtf.getOriginalFilename();
        byte[] bytes = mtf.getBytes();
        Long userId = yTokenShare.get().getUserId();
        String path ="filesystem/"+userId+"/wallpaper/"+ IdUtils.nextId()+name.substring(name.lastIndexOf("."));
        Personality personality =new Personality();
        personality.setUserWallpaperPath(path);
        personality.setUserId(userId);
        personality.setUpdateBy(userId);
        personality.setUpdateTime(LocalDateTime.now());
        personalityMapper.updateWallpaperPath(personality);
        ossUtils.pushFileSource(bytes,path);
    }
}
